package com.hl.controller;

import com.github.pagehelper.util.PageObjectUtil;
import com.hl.domain.Psychologyknowledge;
import com.hl.service.serviceImpl.PsychologyKnowledgeServiceImpl;
import com.hl.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;

@RequestMapping("/psychologyKnowledge")
@RestController
public class PsychologyKnowledgeController {

    @Autowired
    private PsychologyKnowledgeServiceImpl psychologyKnowledgeServiceImpl;


//    查询所有
    @GetMapping("/selectAll")
    public R selectAll (){
        return R.success(psychologyKnowledgeServiceImpl.selectAll());
    }

//    根据name查询
    @GetMapping("/selectByName")
    public R selectByPsychologyname(String name){
        return R.success(psychologyKnowledgeServiceImpl.selectByPsychologyname(name));
    }

//    根据id查询
    @GetMapping("/selectById")
    public R selectById(Long id){
    return R.success(psychologyKnowledgeServiceImpl.selectByPrimaryKey(id));
}

//    新增
    @PostMapping("/insert")
    public R insert(Psychologyknowledge psychologyknowledge){
        return R.success(psychologyKnowledgeServiceImpl.insert(psychologyknowledge));
    }

//    删除
    @DeleteMapping("/delete")
    public R delete(Long id){
        return R.success(psychologyKnowledgeServiceImpl.delete(id));
    }

//    修改
    @PostMapping("/update")
    public R update(Psychologyknowledge psychologyknowledge){
        return R.success(psychologyKnowledgeServiceImpl.update(psychologyknowledge));
    }

//    下载封面
//    @GetMapping("/down")
//    public void down(String psychologyKnowledgeName, HttpServletResponse response) throws IOException {
//
//        FileInputStream in = new FileInputStream("d:\\upload\\" + psychologyKnowledgeName);
//        ServletOutputStream out = response.getOutputStream();
//
//        byte[] b = new byte[1024];
//        int i = in.read(b);
//        while (i != -1){
//            out.write(b, 0, i);
//            i = in.read(b);
//        }
//        out.close();
//        in.close();
//    }
@GetMapping("/down")
public void down(String filename, HttpServletResponse response) throws
        IOException {
    // 原生servlet
    File file = new File(filename);
    InputStream inputStream = new FileInputStream(file);
    ServletOutputStream outputStream = response.getOutputStream();
    FileCopyUtils.copy(inputStream,outputStream);

}

//    upload
@PostMapping("/upload")
public R upload(MultipartFile file) throws IOException {
    String path="D:\\upload\\";
    String originalFilename = file.getOriginalFilename();
    String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
    String filename = UUID.randomUUID().toString()+ext;
    path += filename;

    file.transferTo(new File(path));

    return R.success("http://localhost:8080//psychologyKnowledge/down?filename=D:/upload/"+filename);
    }
}


